﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IPromotionService : IAppService
    {
        List<Promotion> GetList(string Title, int ST);


        List<Promotion> GetPromotionsByProductID(string ProductID, int ST);
        Promotion Add(AddPromotionInput input);
        void Update(UpdatePromotionInput input);
        void Delete(string id );

        Promotion GetModel(string Id); 

        PagedData<Promotion> GetPageData(Pagination page, string keyword, int ST);

        PagedData<PromotionInfo> GetPromotionInfoList(Pagination page, string keyword, int ST);
    }

    public class PromotionService : AppServiceBase<Promotion>, IPromotionService
    {
        public PromotionService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<Promotion> GetList(string Title, int ST)
        {
            var q = this.Query;
            if (Title.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Title.Contains(Title));
            }
            if (ST > -1)
            {
                q = q.Where(a => a.ST == ST);
            } 
            var ret = q.ToList();
            return ret;
        }

        public List<Promotion> GetPromotionsByProductID(string ProductID,int ST)
        {
            string sql = "SELECT * FROM promotion WHERE ST=?ST and Id IN(SELECT distinct PromotionId FROM promotion_item WHERE ProductID=?ProductID);";
            List<Promotion> list = this.DbContext.SqlQuery<Promotion>(sql, new { ST = ST, ProductID = ProductID }).ToList();
            return list;
        }

         

        public Promotion Add(AddPromotionInput input)
        {
            input.CreateDate = DateTime.Now;
            return this.InsertFromDto(input);
        }
        public void Update(UpdatePromotionInput input)
        {
            this.UpdateFromDto(input);
        }

        public Promotion GetModel(string Id)
        {
            return  this.Query.Where(a => a.Id == Id).FirstOrDefault();
        }

        public void Delete(string id)
        {
            this.DbContext.Delete<Promotion>(a => a.Id == id);
        }


        public PagedData<Promotion> GetPageData(Pagination page, string keyword, int ST)
        {
            var q = this.DbContext.Query<Promotion>();            
            if (keyword.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Title == keyword);
            }
            if (ST > -1)
            {
                q = q.Where(a => a.ST == ST);
            }
            PagedData<Promotion> pagedData = q.TakePageData(page);
            return pagedData;
        }




        public PagedData<PromotionInfo> GetPromotionInfoList(Pagination page, string keyword, int ST)
        {
            string strFileds = "a.*";

            string strWhere = "1=1 ";

            if(ST>-1)
            {
                strWhere += " and a.ST="+ST;
            }
             
            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere += " and a.Title like '%" + keyword + "%'";
            }

            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", strFileds),
                new DbParam("?_tables", " promotion a "),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", "a.CreateDate desc"),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
        };

            List<PromotionInfo> list = this.DbContext.SqlQuery<PromotionInfo>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();

            PagedData<PromotionInfo> pagedData = new PagedData<PromotionInfo>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);

            return pagedData;
        }



    }

}
